package vrm

import (
	"bytes"
	"encoding/json"
	"errors"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"vrcm/src/vrcm/app/admin/models"
	dto "vrcm/src/vrcm/app/admin/service/dto/vrm"
	"vrcm/src/vrcm/common/service"
	ext "vrcm/src/vrcm/envconfig"
)

var client = http.Client{}

type VrmService struct {
	service.Service
}

// WSQuery
func (e *VrmService) WSQuery(d *dto.WsListReq) (dto.WsListRes, error) {

	res := dto.WsListRes{}
	urlString := ext.ExtConfig.ServiceConfig.VrmServiceUrl + "/api/v1/cloud/storage/WSGetList"
	//urlString := "https://vrm.lab.videosolar.com" + "/api/v1/cloud/storage/WSGetList"
	// 参数
	data := make(map[string]interface{})
	data["tenantId"] = d.TenantId
	data["bindId"] = d.BindId
	data["pageSize"] = d.PageSize
	data["pageIndex"] = d.PageIndex
	data["isVideo"] = d.IsVideo
	// Marshal
	bytesData, _ := json.Marshal(data)
	e.Log.Trace(string(bytesData))
	// 获取 request请求
	request, err := http.NewRequest("POST", urlString, bytes.NewReader(bytesData))
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return res, err
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return res, err
	}
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	e.Log.Trace("WSQuery:", string(body))
	result := gjson.Parse(string(body))

	if result.Get("code").Int() != http.StatusOK {
		return res, errors.New(result.Get("message").String())
	}

	err = json.Unmarshal(body, &res)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return res, err
	}
	return res, nil
}

//// WSQuery
//func (e *VrmService) WSDelete(d *dto.WSDeleteReq) error {
//
//	res := dto.WsListRes{}
//	urlString := ext.ExtConfig.ServiceConfig.VrmServiceUrl + "/api/v1/cloud/storage/WSGetList"
//	//urlString := "https://vrm.lab.videosolar.com" + "/api/v1/cloud/storage/WSGetList"
//	// 参数
//	data := make(map[string]interface{})
//	data["tenantId"] = d.TenantId
//	data["bindId"] = d.BindId
//	data["pageSize"] = d.PageSize
//	data["pageIndex"] = d.PageIndex
//	// Marshal
//	bytesData, _ := json.Marshal(data)
//	e.Log.Trace(string(bytesData))
//	// 获取 request请求
//	request, err := http.NewRequest("POST", urlString, bytes.NewReader(bytesData))
//	request.Header.Set("Authorization", "Bearer "+models.SuperToken)
//	request.Header.Set("X-PS-Flag", "")
//	request.Header.Add("Content-Type", "application/json")
//	if err != nil {
//		e.Log.Errorf("GetHttpSkip Request Error:", err)
//		return res, err
//	}
//	response, err := client.Do(request)
//	if err != nil {
//		e.Log.Errorf("GetHttpSkip Response Error:", err)
//		return res, err
//	}
//	defer response.Body.Close()
//	body, err := ioutil.ReadAll(response.Body)
//	if err != nil {
//		e.Log.Errorf("读取响应体失败 error:%s", err)
//		return res, err
//	}
//	e.Log.Trace("WSQuery:", string(body))
//	result := gjson.Parse(string(body))
//
//	if result.Get("code").Int() != http.StatusOK {
//		return res, errors.New(result.Get("message").String())
//	}
//
//	err = json.Unmarshal(body, &res)
//	if err != nil {
//		e.Log.Errorf("读取响应体失败 error:%s", err)
//		return res, err
//	}
//	return res, nil
//}
